package source;

/*
 * Author: Aqeel Kazmi
 * Email: aqeelhkazmi@gmail.com
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

import javax.media.*;

public class ShowFiles extends MouseAdapter implements ActionListener {
	
	Frame d = null;
	MediaPlayerFrame mpf = null;
	java.awt.List songs = null;
	Properties ht = null;
	String name = null;
	JButton btsetfolder = null;
	JButton btplay = null;
	JButton btrefresh = null;
	JButton btclose = null;
	
	String path = null;
	int playing = 0;
	
	public ShowFiles(MediaPlayerFrame mpf)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		this.mpf = mpf;
		songs = new java.awt.List(10,false);
		songs.addMouseListener(this);
		ht = new Properties();
		d = new Frame();
		name = "Available Files";
		setButtons();
		process();		
		d.setSize(270,300);
		d.setResizable(false);
	}
	
	public void setButtons()
	{
		btsetfolder = new JButton("Set Folder");
		btsetfolder.setActionCommand("Set Folder");
		btsetfolder.addActionListener(this);
		
		btplay = new JButton("Play");
		btplay.setActionCommand("Play");
		btplay.addActionListener(this);
		
		btrefresh = new JButton("Refresh");
		btrefresh.setActionCommand("Refresh");
		btrefresh.addActionListener(this);
		
		btclose = new JButton("Close");
		btclose.setActionCommand("Close");
		btclose.addActionListener(this);
		
	}
	public void showList()
	{	
		
		d.setLocation(350,150);
		d.setVisible(true);
	}
	public void add()
	{

		try{
			path = GetFile();
			File fi = new File(path);
			String sf[]= fi.list();
			for(int i=0; i<sf.length; i++){
				ht.put(String.valueOf(songs.getItemCount()),sf[i]);			
				songs.add(sf[i]);
			}
	    	}catch (Exception e){
	    		System.out.println(e);
	    	}

		process();
		d.setVisible(true);
	}
	public void process()
	{
		d.removeAll();
		d.setLayout(new BoxLayout(d,BoxLayout.X_AXIS));	
		d.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{
				d.setVisible(false);
			}
		}
		);
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));

		JPanel bt = new JPanel();
		bt.setLayout(new BoxLayout(bt,BoxLayout.X_AXIS));
		
		btsetfolder.updateUI();
		btplay.updateUI();
		btrefresh.updateUI();
		btclose.updateUI();

		bt.add(btsetfolder);
		bt.add(btplay);
		bt.add(btrefresh);
		bt.add(btclose);
		
		jp.add(songs);
		jp.add(bt);
		
		d.setTitle(name);
		d.setFocusable(true);
		d.addKeyListener(new KeysListen(mpf));				
		d.add(jp);		
		
	}
	
	public void playSong(int index)
	{	
		String ss = (String)ht.get(String.valueOf(index));
		if(ss==null)
		return;
		try
		{	
		ss=path+ss;
		playing = index;
		//mpf.setTitle(songs.getItem(playing)+" - "+mpf.FRAME_TITLE);						
		MediaLocator ml = new MediaLocator((new File(ss)).toURL());
		mpf.setLocator(ml);		
		}
		catch(Exception e){
			System.out.println(e);
			System.out.println(ss);
		}
	}

	public int totalSongs()
	{		
		
		int count = songs.getItemCount();
		return count;
	}

	public void refresh()
	{
			
		songs.clear();
		File fi = new File(path);
		String sf[]= fi.list();
		for(int i=0; i<sf.length; i++){
			ht.put(String.valueOf(songs.getItemCount()),sf[i]);			
			songs.add(sf[i]);
		}
		process();
		d.setVisible(true);
		
	}
	public void nextSong()
	{		
		try
			{
		if((playing+1)>=totalSongs())
		playing = 0;		
		else
		playing++;		
		playSong(playing);		
		}
		catch(Exception e)
	{
		System.out.println(e);
	}
	}
	
    String GetFile()
	{
	
	String ret=null;
	FileDialog fd = new FileDialog(d,"Open File",FileDialog.LOAD);
	fd.setDirectory("");
	fd.setVisible(true);
	if (fd.getFile() != null) {
	ret = fd.getDirectory();
	}
	return ret;
	}
    public void close(){
    	
    	d.dispose();
    	System.exit(0);
    }


	public void actionPerformed(ActionEvent ae)
	{
		String check = ae.getActionCommand();
		if(check.equals("Refresh"))
		{
			refresh();
		}
		
		if(check.equals("Close"))
			{
			d.setVisible(false);
			}
		if(check.equals("Set Folder")){
			add();
		}
		if(check.equals("Play"))
		{		
			if(songs.getSelectedIndex()==-1)
			return;			
			playing = songs.getSelectedIndex();
			playSong(playing);

		}
	}

	public void mouseClicked(MouseEvent me)
	{
		if(me.getClickCount()==2 && me.getButton()==1)
		{
		if(songs.getSelectedIndex()==-1)
		return;			
		playing = songs.getSelectedIndex();
		playSong(playing);
		}
		else
		return;
	}

}
